package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.PerMissionMapper;
import com.atguigu.ssyx.acl.service.PerMissionService;
import com.atguigu.ssyx.acl.service.RolePermissionService;
import com.atguigu.ssyx.acl.utils.PermissionHelper;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.model.acl.Permission;
import com.atguigu.ssyx.model.acl.RolePermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class PerMissionServiceImpl extends ServiceImpl<PerMissionMapper,Permission> implements PerMissionService {

    @Resource
    private RolePermissionService rolePermissionService;



    //查询所有菜单
    @Override
    public List<Permission> queryAllPermission() {
        //1 查询所有菜单
        List<Permission> allPermissionList =
                baseMapper.selectList(null);

        //2 转换要求数据格式
        List<Permission> result = PermissionHelper.buildPermission(allPermissionList);
        return result;
    }

    //递归删除菜单
    @Override
    public void removeChildById(Long id) {
        //1 创建list集合idList，封装所有要删除菜单id
        List<Long> idList = new ArrayList<>();

        //根据当前菜单id，获取当前菜单下面所有子菜单，
        //如果子菜单下面还有子菜单，都要获取到
        //重点：递归找当前菜单子菜单
        this.getAllPermissionId(id,idList);

        //设置当前菜单id
        idList.add(id);

        //调用方法根据多个菜单id删除
        baseMapper.deleteBatchIds(idList);
    }



    //重点：递归找当前菜单下面的所有子菜单
    //第一个参数是当前菜单id
    //第二个参数最终封装list集合，包含所有菜单id
    private void getAllPermissionId(Long id, List<Long> idList) {
        //根据当前菜单id查询下面子菜单
        //select * from permission where pid=2
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Permission::getPid,id);
        List<Permission> childList = baseMapper.selectList(wrapper);

        //递归查询是否还有子菜单，有继续递归查询
        childList.stream().forEach(item -> {
            //封装菜单id到idList集合里面
            idList.add(item.getId());
            //递归
            this.getAllPermissionId(item.getId(),idList);
        });
    }



    //获取所有菜单，和根据角色id查询角色分配菜单列表
    @Override
    public Map<String, Object> getPermissionRoleId(Long roleId) {

        //1 查询所有角色
       List<Permission> allPermissions = baseMapper.selectList(null);

        //2 根据用户id查询用户分配角色列表
        //2.1 根据用户id查询 用户角色关系表 admin_role 查询用户分配角色id列表
        // List<Permission>
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();

        //设置查询条件，根据角色id roleId
        wrapper.eq(RolePermission::getPermissionId,roleId);
        List<RolePermission> permissionList = rolePermissionService.list(wrapper);

        //2.2 通过第一步返回的集合，获取所有角色id的列表  List<RolePermission>---List<Long>
        List<Long> permissionIdList = permissionList
                .stream()
                .map(item->item.getPermissionId())
                .collect(Collectors.toList());

        //2.3 创建新的list集合，用于存储角色配置菜单
        List<Permission> assignPermissionList =  new ArrayList<>();
        //2.4 遍历所有角色列表 allRolesList， 得到每个角色
        //判断所有角色里面是否包含已经分配角色id，封装到2.3里面新的list集合
        for (Permission permission:allPermissions
             ) {
            if(permissionIdList.contains(permission.getId())){
                assignPermissionList.add(permission);
            }
        }
            //封装到map返回
        Map<String,Object> result = new HashMap<>();
        //所有菜单列表
        result.put("allPermissions",allPermissions);
        //角色分配菜单列表
        result.put("assignPermissions",assignPermissionList);
        return result;
    }


    //为角色进行分配菜单
    @Override
    public void saveRolePermission(Long roleId, Long[] permissionIds) {
        //1 删除用户已经分配过的角色数据
        //根据角色id删除role_permission表里面对应数据
                LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(RolePermission::getPermissionId,roleId);
                rolePermissionService.remove(wrapper);
        //2 重新分配
        //遍历多个角色id，得到每个角色id，拿着每个角色id + 用户id添加用户角色关系表
        List<RolePermission> list = new ArrayList<>();
        for (Long permissionId : permissionIds
             ) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);

            //放到list集合
            list.add(rolePermission);
        }

        //调用方法添加
        rolePermissionService.saveBatch(list);

    }
}












